package cn.tedu.boyou.website.controller;

import cn.tedu.boyou.acticle.domain.Sight;
import cn.tedu.boyou.acticle.service.ISightService;
import cn.tedu.boyou.cache.prefix.SightStatisKeyPrefix;
import cn.tedu.boyou.cache.service.ISightStatisRedisService;
import cn.tedu.boyou.comment.domain.ReviewComment;
import cn.tedu.boyou.comment.domain.SightReview;
import cn.tedu.boyou.comment.service.IReviewCommentService;
import cn.tedu.boyou.comment.service.ISightReviewService;
import cn.tedu.boyou.common.query.SightQuery;
import cn.tedu.boyou.common.query.SightReviewQuery;
import cn.tedu.boyou.common.util.AjaxResult;
import cn.tedu.boyou.member.domain.UserInfo;
import cn.tedu.boyou.website.annotation.RequireLogin;
import cn.tedu.boyou.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ReviewController {

    @Reference
    private ISightReviewService sightReviewService;
    @Reference
    private ISightService sightService;
    @Reference
    private ISightStatisRedisService sightStatisRedisService;
    @Reference
    private IReviewCommentService reviewCommentService;
    @RequestMapping("/sight")
    public String review(@ModelAttribute("qo") SightQuery qo,Model model){
        List<Sight> sights = sightService.queryTop5(qo);

        //根据景点的id查询查询相应点评的条数共享到页面
        for(Sight sight:sights){
            int reviewNum = sightStatisRedisService.getReviewNum(SightStatisKeyPrefix.SIGHT_REVIEW_COUNT, sight.getId());
            sight.setReviewnum(reviewNum);

        }
        model.addAttribute("sights",sights);

        return "destination/sightList";
    }

    @RequestMapping("/sight/getSightByDestId")
    @ResponseBody
    public List<Sight> getSightByDestId(Long id){
        int num = 3;
        List<Sight> sights = sightService.queryTop3(id,num);
        return sights;
    }

    @RequestMapping("/detail")
    public String Detail(SightReviewQuery qo,Model model)  {
         //根据景点的id查询景点对象共享到页面
        Sight sight = sightService.selectBySightId(qo.getSightId());

        model.addAttribute("sight",sight);
        //根据景点的id查询查询相应点评的条数共享到页面
        int reviewNum = sightStatisRedisService.getReviewNum(SightStatisKeyPrefix.SIGHT_REVIEW_COUNT, qo.getSightId());
        model.addAttribute("reviewNum",reviewNum);
        //从redis中将对应景点的收藏数查询出来
        int favorNum = sightStatisRedisService.getFavorNum(SightStatisKeyPrefix.SIGHT_REVIEW_COUNT, qo.getSightId());
        model.addAttribute("favorNum",favorNum);
        //根据景点所在的id查询景点所在的城市
        String city = sightService.getCityBySightId(qo.getSightId());
        //后台通过接口发送天气查询
        sightService.sendWeather(city);
        return "destination/reviewDetail";
    }

    @RequestMapping("/reviewPage")
    public String comment(@ModelAttribute("qo") SightReviewQuery qo, Model model){
        Page<SightReview> page = sightReviewService.query(qo);
        model.addAttribute("page",page);
        model.addAttribute("divReviewNum",page.getSize());
        return "destination/reviewTpl";
    }

    @RequestMapping("/addReview")
    @ResponseBody
    //@RequireLogin
    public Object addReview(SightReview sightReview, @UserParam UserInfo userInfo){
        if(userInfo == null){
            return new AjaxResult("请您先登录");
        }


        //保存登录用户的信息到sightReview对象中
        sightReview.setHeadUrl(userInfo.getHeadImgUrl());
        sightReview.setUserId(userInfo.getId());
        sightReview.setLevel(userInfo.getLevel());
        sightReview.setUsername(userInfo.getNickname());

        //保存sightReview对象
        sightReviewService.save(sightReview);

        //每一个评论的增加，在redis缓存中增加对应的sight评论数
        sightStatisRedisService.increaseReviewNum(sightReview.getSightId(),1);
        return new AjaxResult();
    }

     @RequestMapping("/addThumb")
     @RequireLogin
     @ResponseBody
    public Object thumbUp(String rid,@UserParam UserInfo userInfo){
        //将点赞用户id保存到对应景点点评的userIds集合中，表示点赞成功
         if(userInfo == null){
             return new AjaxResult("请先登录");
         }
         Long uid = userInfo.getId();
         sightReviewService.thumbUp(rid,uid);
         int thumbNum = sightReviewService.getSightReviewById(rid).getThumbNum();
         return new AjaxResult().addData(thumbNum);
    }

    @RequestMapping("/reviewComment")
    @RequireLogin
    public String reviewComment(ReviewComment reviewComment, @UserParam UserInfo userInfo, Model model){

        //将回复的用户信息设置到reviewComment
        reviewComment.setUserId(userInfo.getId());
        reviewComment.setUsername(userInfo.getNickname());
        reviewComment.setHeadUrl(userInfo.getHeadImgUrl());

        //将用户信息保存到mongoDB中
        ReviewComment comment = reviewCommentService.save(reviewComment);
        model.addAttribute("rc",comment);
        return "/destination/replyTpl";
    }

    //收藏功能
    @RequireLogin
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sightId ,Model model,@UserParam UserInfo userInfo){
        if(userInfo == null){
            return new AjaxResult("请先登录");
        }
         //调用统计方法，存入相应的统计结果
        Long uid = userInfo.getId();
        boolean ret = sightStatisRedisService.addFavor(SightStatisKeyPrefix.SIGHT_FAVOR_COUNT, sightId, uid);
        //从redis中将对应景点的收藏数查询出来
        int favorNum = sightStatisRedisService.getFavorNum(SightStatisKeyPrefix.SIGHT_REVIEW_COUNT, sightId);
        return  new AjaxResult().addData(favorNum);
    }




}
